﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Globalization;
using GraphSample.CustomControls;

namespace GraphSample
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            statusText.Text = "Be patient generating large random graphs";
            graph1.ChartTitle = "No Data";
        }

        private void lineGraph_Click(object sender, RoutedEventArgs e)
        {
            graph1.LineGraph = true;
        }

        private void fillGraph_Click(object sender, RoutedEventArgs e)
        {
            graph1.LineGraph = false;
        }

        private void msftButton_Click(object sender, RoutedEventArgs e)
        {
            List<IGraphData> graphData = LoadStockData();
            graph1.GraphData  = graphData;
            graph1.ChartTitle = "Historical MSFT Stock Price";
            statusText.Text   = "Done";
        }

        private List<IGraphData> LoadStockData()
        {
            CultureInfo enCulture = new CultureInfo("en-US");
            List<IGraphData> graphData = new List<IGraphData>();
            string[] lines = SampleData.MSFTData.Split(new char[] { '\n' });
            foreach (string line in lines)
            {
                string[] tokens = line.TrimEnd().Split(new char[] { ',' });
                DateTime date = DateTime.Parse(tokens[0], CultureInfo.InvariantCulture);
                float close = float.Parse(tokens[6], enCulture);
                graphData.Add(new CloseData(date, close));
            }
            graphData.Sort();
            return graphData;
        }

        private void generateButton_Click(object sender, RoutedEventArgs e)
        {
            statusText.Text = "Generating graph data";
            int numDataPoints = 0;
            if (!Int32.TryParse(numPoints.Text, out numDataPoints) || numDataPoints <= 0)
            {
                statusText.Text = "Please enter a valid integer for number of points";
            }
            else if (numDataPoints > 1000000)
            {
                statusText.Text = "Data points must be < 1 million due to memory";
            }
            else
            {
                List<IGraphData> graphData = GenerateGraph(numDataPoints); ;
                graph1.GraphData  = graphData;
                graph1.ChartTitle = String.Format("{0} Random Points", numDataPoints);
                statusText.Text   = "Done";
            }
        }

        private List<IGraphData> GenerateGraph(int numDataPoints)
        {
            Random rand = new Random();
            List<IGraphData> graphData = new List<IGraphData>();

            float lastY = 50;
            float targetY = 0;
            int pointsThisRound = 0;
            int currX = 1;
            while (currX < numDataPoints)
            {
                pointsThisRound = (int)(rand.NextDouble() * 100);
                pointsThisRound = Math.Min(pointsThisRound, numDataPoints - currX);
                targetY = (float)(rand.NextDouble() * 80) + 10;
                for (; pointsThisRound > 0; pointsThisRound--)
                {
                    float nextY = (targetY - lastY) / pointsThisRound;
                    nextY *= (float)(rand.NextDouble() + 0.5);
                    nextY += lastY;
                    graphData.Add(new BasicGraphData(currX, nextY));
                    lastY = nextY;
                    currX++;
                }
            }
            return graphData;
        }
    }
}